//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    ky: '',
    searchData: []
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
   
  },
  onShow: function () {
    var that = this;
    var searchData = wx.getStorageSync('searchData');
    console.log(searchData)
    that.setData({
      searchData: searchData
    })
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  getky: function (e) {
    var that = this;
    var ky = e.detail.value;
    that.setData({
      ky: ky
    })
  },

  schquan: function () {
    var ky = this.data.ky;
    if (ky != '') {
      var searchData = wx.getStorageSync('searchData') || []


      var hasone=searchData.indexOf(ky)
      if(hasone!==-1){
        searchData.splice(hasone, 1)

      }
      searchData.unshift(ky)
      wx.setStorageSync('searchData', searchData)
      wx.redirectTo({
        url: '/pages/shengyiquan/search-result/search-result?term=' + ky
      })
    }
  },
  deleteHistory: function () {
    var that = this
    wx.showModal({
      title: '提示',
      content: '是否删除历史搜索',
      success: function (res) {
        if (res.confirm) {
          wx.setStorageSync('searchData', []);
          that.setData({
            searchData: []
          })
         
        }
      }
    })
  }
})
